﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace Marmat.Data
{

    /// <summary>
    /// 表示一个数据源集合
    /// </summary>
    /// <remarks></remarks>
    [DefaultProperty("Name")]
    [DefaultMember("Item")]
    public abstract class Connections
    {


        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        public static Dictionary<string, BaseConnection> Items
        {
            get
            {
                if (m_Items is null)
                {
                    m_Items = new Dictionary<string, BaseConnection>();
                }

                return m_Items;
            }
        }

        internal static Dictionary<string, BaseConnection> m_Items;

        /// 返回一个数据源
        /// </summary>
        /// <param name="ConnectionName">数据源名称</param>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public static BaseConnection Item(string ConnectionName)
        {
            if (!string.IsNullOrEmpty(ConnectionName))
            {
                if (Items.ContainsKey(ConnectionName))
                {
                    var Connection = Items[ConnectionName];
                    return Connection;
                }
            }
            return null;
        }

        /// <summary>
        /// 返回一个数据源
        /// </summary>
        /// <param name="index"></param>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public static BaseConnection get_Item(int index)
        {
            int @int = 0;
            foreach (string Key in Items.Keys)
            {
                if (index == @int)
                {
                    return Item(Key);
                }
                @int = 1;
            }

            return null;
        }

        /// <summary>
        /// 返回数据源的数量
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public static int Count
        {
            get
            {
                return Items.Count;
            }
        }

        public static BaseConnection Default
        {
            get
            {
                if (m_Default is null && Count > 0)
                {
                    return get_Item(0);
                }

                return m_Default;
            }

            set
            {
                m_Default = value;
            }
        }

        private static BaseConnection m_Default;
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        public static bool Contains(string ConnectionName)
        {
            return Items.ContainsKey(ConnectionName);
        }

        /// <summary>
        /// 添加数据源
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="connectionString"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public static BaseConnection Add(string connectionName, string connectionString, SourceType sourceType)
        {
            var Connection = new Connection(connectionString, sourceType);
            Connection.Name = connectionName;
            if (Add(connectionName, Connection))
            {
                return Connection;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 添加数据源
        /// </summary>
        /// <param name="ConnectionName"></param>
        /// <param name="Connection"></param>
        /// <returns></returns>
        public static bool Add(string ConnectionName, BaseConnection Connection)
        {
            if (!string.IsNullOrEmpty(ConnectionName))
            {
                if (Items.ContainsKey(ConnectionName))
                {
                    throw new Exception("系统已存在名为\"" + ConnectionName + "\"的数据源,因此添加失败!");
                }
                else
                {
                    Items.Add(ConnectionName, Connection);
                    return true;
                }
            }
            else
            {
                throw new Exception("数据源名称不能为空。");
            }

            return false;
        }

        public static void Delete(string Name)
        {
            if (Items.ContainsKey(Name))
            {
                Items.Remove(Name);
            }
            else
            {
                throw new Exception("系统未找到名为\"" + Name + "\"的数据源,因此删除失败!");
            }
        }
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
    }
}